﻿using DryIoc;
using OpenClassic.Server.Configuration;
using OpenClassic.Server.Networking;
using System;

namespace OpenClassic.Server
{
    public class Program
    {
        private static readonly NettyGameServer Server;
        private static readonly IGameEngine Engine;

        static Program()
        {
            var resolver = DependencyResolver.Current;

            Server = resolver.Resolve<NettyGameServer>();
            Engine = resolver.Resolve<IGameEngine>();
        }

        //private static async Task InitServer()
        //{
        //    var server = new DefaultGameServer();

        //    await Task.CompletedTask;
        //}

        //public static void Main(string[] args)
        //{
        //    AsyncContext.Run(InitServer);

        //    for (;;)
        //    {
        //        Thread.Sleep(100);
        //    }
        //}

#pragma warning disable RECS0154 // Parameter is never used
        public static void Main(string[] args)
#pragma warning restore RECS0154 // Parameter is never used
        {
            Console.WriteLine("Starting listener...");
            Server.Start().Wait();
            Console.WriteLine("Listening");

            Console.CancelKeyPress += Console_CancelKeyPress;

            GC.Collect();

            // This call blocks the main thread until the server is shut down.
            Engine.GameLoop();

            Console.WriteLine("Game loop shut down.");
            Console.WriteLine("Shutdown complete.");
        }

        private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            Console.WriteLine("Shutting down socket listener...");
            Server.Stop().Wait();

            Console.WriteLine("Shutting down game loop...");
            Engine.StopGameLoop();
        }
    }
}
